import axios from 'axios'
import NProgress from 'nprogress' //进度条
import 'nprogress/nprogress.css'
import { getToken } from './token'
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000 //超时时间
})

// 添加请求拦截器
request.interceptors.request.use(
  (config) => {
    if (getToken()) {
      config.headers['token'] = getToken()
    }
    NProgress.start() //开始进度条
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 添加响应拦截器
request.interceptors.response.use(
  (response) => {
    NProgress.done() // 结束进度条
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default request
